import os
import pandas as pd
import json
dirname = './Network_Trace/log/'
filename = ".xls"
import numpy as np

result = []
def search(dirname=dirname, filename=""):
    for item in os.listdir(dirname):
        item_path = os.path.join(dirname, item)
        # item_path = dirname+item
        if os.path.isdir(item_path):
            search(item_path, filename)
        elif os.path.isfile(item_path):
            if filename in item:
                global result
                if "~$" in item:
                    continue
                result.append(item_path)
                print(item_path+";")
    return result

class ExtendEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, np.int32):
            return int(o)
        elif isinstance(o,np.int64):
            return int(o)
        else:
            return super(ExtendEncoder, self).default(o)

if __name__ == '__main__':
    # search(dirname, filename)
    file_list = search('../Network_Trace/log/', ".xls")

    for i in range(len(file_list)):
        file_list[i] = file_list[i].replace('\\', '/')
        data = pd.read_excel(file_list[i],usecols=["DL_bitrate"])# usecols="DL_bitrate"
        # data.index = ["bandwidth"] * data.shape[0]
        # data.columns = ["DL"]
        # data_json = data.to_json(orient="index", force_ascii=False)
        data_dict = dict(data['DL_bitrate'])
        k = 0
        for j in range(len(data_dict)):
            import copy
            tmp = copy.deepcopy(data_dict[j])
            if data_dict[j] < 80000:
                data_dict.pop(j)
            else:
                data_dict.pop(j)
                data_dict[k] = tmp
                k += 1
        if len(data_dict) < 20:
            continue

        name = file_list[i].split("/")
        json_name = "../Network_Trace/5Glogs/" + name[3] + ".json"
        # with open(json_name, 'w') as json_file:
        #     json.dump(data_dict, json_file, cls=ExtendEncoder, ensure_ascii=False)

        # with open(json_name, 'w', encoding='utf-8') as json_file:
        # with open(json_name, 'w') as json_file:
        write_data = json.dumps(data_dict, cls=ExtendEncoder)
        with open(json_name, 'w') as json_file:
            json_file.write(write_data)
            json_file.close()
    print("end")

        # print(data)

